﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace assignment_1.Models
{
    public partial class UnsavedCanvas
    {
        ContentDBDataContext canvasDB = new ContentDBDataContext();
        DataRepository m_rep = new DataRepository();

        string UnsavedCanvasId { get; set; }
        public const string CanvasSessionKey = "CanvasId";
        public static UnsavedCanvas GetCanvas(HttpContextBase context)
        {
            var canvas = new UnsavedCanvas();
            canvas.UnsavedCanvasId = canvas.GetCanvasId(context);
            return canvas;
        }

        public static UnsavedCanvas GetCanvas(Controller controller)
        {
            return GetCanvas(controller.HttpContext);
        }
        public void AddToCanvas(Canva shape)
        {
            var canvItem = canvasDB.UnsavedCanvas.SingleOrDefault(
                c => c.unsavedID == UnsavedCanvasId
                && c.unsavedCanvasShapes == shape.canvasShapes);

            if (canvItem == null)
            {
                canvItem = new UnsavedCanva
                {
                    unsavedCanvasName = shape.canvasName,
                    unsavedID = UnsavedCanvasId,
                    unsavedCanvasShapes = shape.canvasShapes,
                    unsavedCanvasUserName = shape.userName,
                    unsavedCanvasDate = shape.canvasDate
                };
                canvasDB.UnsavedCanvas.InsertOnSubmit(canvItem);
            }
            else
            {

            }
            canvasDB.SubmitChanges();
        }
        public void RemoveFromCanvas(int id)
        {
            var cartItem = canvasDB.UnsavedCanvas.Single(
                cart => cart.unsavedID == UnsavedCanvasId
                && cart.unsavedCanvasID == id);

            if (cartItem != null)
            {
                canvasDB.UnsavedCanvas.DeleteOnSubmit(cartItem);
                canvasDB.SubmitChanges();
            }
        }
        public void EmptyCanvas()
        {
            var canvasItems = canvasDB.UnsavedCanvas.Where(
                cart => cart.unsavedID == UnsavedCanvasId);

            foreach (var canvItem in canvasItems)
            {
                canvasDB.UnsavedCanvas.DeleteOnSubmit(canvItem);
            }
            canvasDB.SubmitChanges();
        }

        public UnsavedCanva GetCanvasItems()
        {
            return canvasDB.UnsavedCanvas.Where(
                cart => cart.unsavedID == UnsavedCanvasId).OrderByDescending(cart => cart.unsavedCanvasDate).Take(1).SingleOrDefault();
        }

        public string GetCanvasId(HttpContextBase context)
        {
            if (context.Session[CanvasSessionKey] == null)
            {
                if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
                {
                    context.Session[CanvasSessionKey] =
                        context.User.Identity.Name;
                }
                else
                {
                    Guid tempCanvasId = Guid.NewGuid();
                    context.Session[CanvasSessionKey] = tempCanvasId.ToString();
                }
            }
            return context.Session[CanvasSessionKey].ToString();
        }

        public void MigrateCart(string userName)
        {
            var shoppingCart = canvasDB.UnsavedCanvas.Where(
                c => c.unsavedID == UnsavedCanvasId);

            foreach (UnsavedCanva item in shoppingCart)
            {
                item.unsavedCanvasUserName = userName;
            }
            canvasDB.SubmitChanges();
        }
    }
}